//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SphereCylinderOptimizer.h"
using namespace Cephei::QL::Math::Optimization;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (Double r, Double s, Double alpha, Double z1, Double z2, Double z3, Microsoft::FSharp::Core::FSharpOption<Double>^ zweight) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phSphereCylinderOptimizer = NULL;
#endif
        QuantLib::Real _r = (QuantLib::Real)ValueHelper::Convert (r); //d
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //d
        QuantLib::Real _alpha = (QuantLib::Real)ValueHelper::Convert (alpha); //d
        QuantLib::Real _z1 = (QuantLib::Real)ValueHelper::Convert (z1); //d
        QuantLib::Real _z2 = (QuantLib::Real)ValueHelper::Convert (z2); //d
        QuantLib::Real _z3 = (QuantLib::Real)ValueHelper::Convert (z3); //d
        QuantLib::Real _zweight = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (zweight) ? (QuantLib::Real)ValueHelper::Convert (zweight->Value) : 1.0); //4
        _ppSphereCylinderOptimizer = new boost::shared_ptr<QuantLib::SphereCylinderOptimizer> (new QuantLib::SphereCylinderOptimizer ( _r,  _s,  _alpha,  _z1,  _z2,  _z3,  _zweight ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (boost::shared_ptr<QuantLib::SphereCylinderOptimizer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSphereCylinderOptimizer = NULL;
#endif
	_ppSphereCylinderOptimizer = &childNative;
    
}
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (QuantLib::SphereCylinderOptimizer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSphereCylinderOptimizer = NULL;
#endif
	_ppSphereCylinderOptimizer = new boost::shared_ptr<QuantLib::SphereCylinderOptimizer> (&childNative);
    
    _SphereCylinderOptimizerOwner = owner;
    
}

Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (CSphereCylinderOptimizer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSphereCylinderOptimizer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSphereCylinderOptimizer = new boost::shared_ptr<QuantLib::SphereCylinderOptimizer> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSphereCylinderOptimizer = NULL;
#endif
	if (!t->IsSubclassOf(CSphereCylinderOptimizer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (QuantLib::Handle<QuantLib::SphereCylinderOptimizer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSphereCylinderOptimizer = &childNative;
	_ppSphereCylinderOptimizer = &static_cast<boost::shared_ptr<QuantLib::SphereCylinderOptimizer>>(childNative.currentLink());
    
    _SphereCylinderOptimizerOwner = owner;
}
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (QuantLib::Handle<QuantLib::SphereCylinderOptimizer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSphereCylinderOptimizer = &childNative;
	_ppSphereCylinderOptimizer = &static_cast<boost::shared_ptr<QuantLib::SphereCylinderOptimizer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::CSphereCylinderOptimizer (QuantLib::SphereCylinderOptimizer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSphereCylinderOptimizer = NULL;
#endif
	_ppSphereCylinderOptimizer = new boost::shared_ptr<QuantLib::SphereCylinderOptimizer> (new QuantLib::SphereCylinderOptimizer (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::~CSphereCylinderOptimizer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSphereCylinderOptimizer != NULL)
    {
	    delete _ppSphereCylinderOptimizer;
        _ppSphereCylinderOptimizer = NULL;
    }
}
Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::!CSphereCylinderOptimizer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSphereCylinderOptimizer != NULL)
    {
	    delete _ppSphereCylinderOptimizer;
    }
}
QuantLib::SphereCylinderOptimizer& Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::GetReference ()
{
    if (_ppSphereCylinderOptimizer == NULL) throw REFNEW NativeNullException ();
	return **_ppSphereCylinderOptimizer;
}
boost::shared_ptr<QuantLib::SphereCylinderOptimizer>& Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::GetShared ()
{
    if (_ppSphereCylinderOptimizer == NULL) throw REFNEW NativeNullException ();
	return *_ppSphereCylinderOptimizer;
}
QuantLib::SphereCylinderOptimizer* Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::GetPointer ()
{
    if (_ppSphereCylinderOptimizer == NULL) throw REFNEW NativeNullException ();
	return &**_ppSphereCylinderOptimizer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SphereCylinderOptimizer>& Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::GetHandle ()
{
	if (_phSphereCylinderOptimizer == NULL)
	{
		_phSphereCylinderOptimizer = new Handle<QuantLib::SphereCylinderOptimizer> (*_ppSphereCylinderOptimizer);
	}
	return *_phSphereCylinderOptimizer;
}
#endif
bool Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::HasNative () 
{
	return (_ppSphereCylinderOptimizer != NULL);
}

Boolean Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::FindByProjection (Double y1, Double y2, Double y3)
{
    try
    {
        QuantLib::Real _y1 = (QuantLib::Real)ValueHelper::Convert (y1); //a
        QuantLib::Real _y2 = (QuantLib::Real)ValueHelper::Convert (y2); //a
        QuantLib::Real _y3 = (QuantLib::Real)ValueHelper::Convert (y3); //a
    	bool _rv = (bool)(*_ppSphereCylinderOptimizer)->findByProjection ( _y1,  _y2,  _y3 );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::ISphereCylinderOptimizer^ Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::FindClosest (UInt64 maxIterations, Double tolerance, Double y1, Double y2, Double y3)
{
    try
    {
        QuantLib::Size _maxIterations = (QuantLib::Size)ValueHelper::Convert (maxIterations); //a
        QuantLib::Real _tolerance = (QuantLib::Real)ValueHelper::Convert (tolerance); //a
        QuantLib::Real _y1 = (QuantLib::Real)ValueHelper::Convert (y1); //a
        QuantLib::Real _y2 = (QuantLib::Real)ValueHelper::Convert (y2); //a
        QuantLib::Real _y3 = (QuantLib::Real)ValueHelper::Convert (y3); //a
    	(*_ppSphereCylinderOptimizer)->findClosest ( _maxIterations,  _tolerance,  _y1,  _y2,  _y3 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Optimization::CSphereCylinderOptimizer::IsIntersectionNonEmpty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppSphereCylinderOptimizer)->isIntersectionNonEmpty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::ISphereCylinderOptimizer^ Cephei::QL::Math::Optimization::CSphereCylinderOptimizer_Factory::Create (Double r, Double s, Double alpha, Double z1, Double z2, Double z3, Microsoft::FSharp::Core::FSharpOption<Double>^ zweight)
{
    return REFNEW CSphereCylinderOptimizer ( r,  s,  alpha,  z1,  z2,  z3,  zweight);
}
